package com.young.nyd.service.impl;

import com.young.cms.model.AdministrativeRegion;
import com.young.cms.service.IAdministrativeRegionService;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.nyd.model.UviListhazardous;
import com.young.nyd.model.UviPollutiontreatment;
import com.young.nyd.model.UviSuperviseenterprises;
import com.young.nyd.service.IUviListhazardousService;
import com.young.nyd.service.IUviPollutiontreatmentService;
import com.young.nyd.service.IUviSuperviseenterprisesService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 重点监管企业服务实现
* Created by  on 2018-10-13.
*/
@Service("uviSuperviseenterprisesService")
public class UviSuperviseenterprisesServiceImpl implements IUviSuperviseenterprisesService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务
    @Resource(name = "administrativeRegionService")//行政区划
    IAdministrativeRegionService administrativeRegionService;
    @Resource(name = "uviPollutiontreatmentService")//污染物产生及排放情况
    IUviPollutiontreatmentService pollutiontreatmentService;
    @Resource(name = "uviListhazardousService")
    IUviListhazardousService listhazardousService;

    private static final Logger logger = LoggerFactory.getLogger(UviSuperviseenterprisesServiceImpl.class);

    private static final String MAPPER = "com.young.nyd.mapper.uviSuperviseenterprises";//mapper的namespace

    //获取数据列表
    @Override
    public List<UviSuperviseenterprises> getList(UviSuperviseenterprises obj) throws Exception {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    @Override
    public List<UviSuperviseenterprises> getListBycounty(String county) throws Exception {
        UviSuperviseenterprises obj = new UviSuperviseenterprises();
        obj.setRegioncode(county);
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<UviSuperviseenterprises> getPage(UviSuperviseenterprises obj, Page page) throws Exception {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Override
    public UviSuperviseenterprises get(String id) throws Exception {
        UviSuperviseenterprises rs = dataAccessService.getObject(MAPPER + ".get", id);
        AdministrativeRegion region = administrativeRegionService.get(rs.getRegioncode());
        UviPollutiontreatment pollutiontreatment = new UviPollutiontreatment();
        if (region.getAdminLevel() == 1){
            rs.setProvinceCode(rs.getRegioncode());
        }else if(region.getAdminLevel() == 2){
            rs.setCityCode(rs.getRegioncode());
            rs.setProvinceCode(region.getParentId());
        }else if(region.getAdminLevel() == 3){
            AdministrativeRegion cityRegion =  administrativeRegionService.get(region.getParentId());
            rs.setCountyCode(region.getId());
            rs.setCityCode(region.getParentId());
            rs.setProvinceCode(cityRegion.getParentId());
        }
        /*dataAccessService.getObject(MAPPER + ".get", id)*/
        return rs;
    }

    //统计数量
    @Override
    public int count(UviSuperviseenterprises obj) throws Exception {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(UviSuperviseenterprises obj) throws Exception {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) throws Exception {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }


    //插入
    @Override
    public int insert(UviSuperviseenterprises obj) throws Exception {
        obj.setId(CommonUtil.getUUID());
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<UviSuperviseenterprises> list) throws Exception {
        int i = 0;
        for(UviSuperviseenterprises item : list){
            i += this.insert(item);
        }
        return i;
    }

}